package com.ww.gss.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ww.gss.vo.UserVO;

 

@Component("passwordHelper")
public class PasswordHelper {
    private Logger logger = LoggerFactory.getLogger(PasswordHelper.class);

    private RandomNumberGenerator generator = new SecureRandomNumberGenerator();

    private String algorithmName = "md5";

    private final int hashIterations = 2;

    private final String salt = "gss";

    public void encryptPassword(UserVO user) {

        String newPassword = new SimpleHash(algorithmName, user.getUserPassword(), ByteSource.Util.bytes(salt), hashIterations).toHex();
        user.setUserPassword(newPassword);
    }

    public String encryptString(String str) {

        String newPassword = "";

        try {
            // String salt = generator.nextBytes().toHex();
            newPassword = new SimpleHash(algorithmName, str, ByteSource.Util.bytes(salt), hashIterations).toHex();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newPassword;
    }

    public static void main(String[] args) {

    }
}
